﻿namespace ExamV5.CardInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmAddCard : Page
    {
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private int int_0 = 0;
        private PublicFun publicFun_0 = new PublicFun();

        protected void butQuery_Click(object sender, EventArgs e)
        {
            string strSql = "";
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                strSql = strSql + "insert into CardInfo(UserCode,ExamName,SubjectName,ExamDate,GetCountFen,CardNumber,CardStartDate,CardEndDate,EditUserCode,EditUserName) values('" + this.publicFun_0.CheckString(this.DataGridInfo.Rows[i].Cells[0].Text) + "','" + this.publicFun_0.CheckString(this.DataGridInfo.DataKeys[i]["ExamName"].ToString()) + "','" + this.publicFun_0.CheckString(this.DataGridInfo.DataKeys[i]["Subject"].ToString()) + "','" + this.publicFun_0.CheckString(this.DataGridInfo.Rows[i].Cells[3].Text) + "','" + this.publicFun_0.CheckString(this.DataGridInfo.DataKeys[i]["GetCountFen"].ToString()) + "','" + this.publicFun_0.CheckString(this.DataGridInfo.Rows[i].Cells[4].Text) + "','" + DateTime.Now.ToShortDateString() + "','" + DateTime.Now.AddDays((double) Convert.ToInt32(this.ViewState["CardDate"])).ToShortDateString() + "','" + this.httpCookie_0.Value + "','" + this.httpCookie_1.Value + "');";
                strSql = string.Concat(new object[] { strSql, "update ExamUser set IsCard='Y' where UserCode='", this.publicFun_0.CheckString(this.DataGridInfo.Rows[i].Cells[0].Text), "' and PaperId=", this.int_0, ";" });
            }
            strSql = strSql + "update SysparmInfo set ParmValue='" + this.ViewState["LastCardNumber"].ToString() + "' where ParmMemo='CardInfo'";
            this.publicFun_0.ExecuteSql(strSql);
            base.Response.Write("<script>alert('颁发证书操作成功！')</script>");
            this.Page.Response.Write("<script>setTimeout(function(){window.location='FrmAddCardList.aspx'},500)</script>");
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex == -1)
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[5].Text = DateTime.Now.ToShortDateString() + "—" + DateTime.Now.AddDays((double) Convert.ToInt32(this.ViewState["CardDate"])).ToShortDateString();
                if (this.DataGridInfo.DataKeys[e.Row.RowIndex]["UPID"].ToString().Replace("&nbsp;", "") != "0")
                {
                    e.Row.Cells[2].Text = this.publicFun_0.GetValues("select DeptName from DeptInfo where ID=" + this.publicFun_0.CheckString(this.DataGridInfo.DataKeys[e.Row.RowIndex]["UPID"].ToString().Replace("&nbsp;", "")), "DeptName") + "＼" + e.Row.Cells[2].Text;
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (this.ViewState["SortType"].ToString() != " Desc")
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "UserCode":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "UserName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "JoinDate":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "UserCode":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "UserName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "JoinDate":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                }
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (!(this.ViewState["SortFiled"].ToString() == sortExpression))
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            else if (this.ViewState["SortType"].ToString() == " Desc")
            {
                this.ViewState["SortType"] = " ASC";
            }
            else
            {
                this.ViewState["SortType"] = " Desc";
            }
            this.method_0();
        }

        private void method_0()
        {
            int num2;
            string values = "0";
            values = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='CardFen'", "ParmValue");
            if (values.Trim() == "")
            {
                values = "0";
            }
            string selectCommandText = "";
            selectCommandText = "select a.ExamName,a.Subject,a.GetCountFen,'' as CardNumber,'' as CardDate,a.JoinDate,b.UserName,a.UserCode,a.PaperID,a.ExamName,c.DeptName,c.UPID from ExamUser a,UserInfo b,DeptInfo c  where b.DeptID=c.ID and a.UserCode=b.UserCode ";
            selectCommandText = string.Concat(new object[] { selectCommandText, " and a.GetCountFen>", values, " and (a.Flag='C' or a.Flag='D' or a.Flag='E') and a.IsCard='N' and a.PaperID=", this.int_0 }) + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
            int length = this.ViewState["CardNumber"].ToString().Length;
            int num = Convert.ToInt32(this.ViewState["CardNumber"]);
            string str2 = "";
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                str2 = "";
                if (num.ToString().Length < length)
                {
                    num2 = num.ToString().Length;
                    while (num2 < length)
                    {
                        str2 = str2 + "0";
                        num2++;
                    }
                }
                this.DataGridInfo.Rows[i].Cells[4].Text = this.ViewState["CardGDBH"].ToString() + str2 + num.ToString();
                num++;
            }
            str2 = "";
            if (num.ToString().Length < length)
            {
                for (num2 = num.ToString().Length; num2 < length; num2++)
                {
                    str2 = str2 + "0";
                }
            }
            this.ViewState["LastCardNumber"] = str2 + num.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            this.int_0 = Convert.ToInt32(base.Request["PaperID"].ToString());
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    this.ViewState["SortFiled"] = "UserCode";
                    this.ViewState["SortType"] = " Desc";
                    this.ViewState["CardGDBH"] = this.publicFun_0.GetValues("select ParmName from SysParmInfo where ParmMemo='CardInfo'", "ParmName");
                    this.ViewState["CardNumber"] = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='CardInfo'", "ParmValue");
                    this.butQuery.Attributes.Add("onclick", @"javascript:{if(confirm('此操作将给列表中所有人员颁发证书。\n确认后将无法修改。\n\n请再次确定要颁发证书吗？')==false) return false;}");
                    this.ViewState["CardDate"] = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='CardDate'", "ParmValue");
                    this.method_0();
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

